package com.keldian.myqq.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.nineoldandroids.view.ViewHelper;

/**
 * Created by Administrator on 15-11-19.
 */
public class MyHorizontalScrollView extends HorizontalScrollView {
    private LinearLayout mLayout;

    private ViewGroup mMenuView;

    private ViewGroup mMainView;

    private int mScreenWidth;
    //menu右边距
    private int mMenuRightPadding = 50;
    //menu的宽度
    private int mMenuWidth;

    private boolean flag;

    public MyHorizontalScrollView(Context context) {
        super(context, null);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        mScreenWidth = outMetrics.widthPixels;

        mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, context.getResources().getDisplayMetrics());

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!flag) {
            mLayout = (LinearLayout) getChildAt(0);
            mMenuView = (ViewGroup) mLayout.getChildAt(0);
            mMainView = (ViewGroup) mLayout.getChildAt(1);

            mMenuWidth = mMenuView.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
            mMainView.getLayoutParams().width = mScreenWidth;
            flag = true;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            this.scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (scrollX > mMenuWidth / 2) {
                    this.smoothScrollTo(mMenuWidth, 0);
                } else {
                    this.smoothScrollTo(0, 0);
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale = l * 1.0f / mMenuWidth;

        float rightScale = 0.7f + 0.3f * scale;
        float leftScale = 1.0f - scale * 0.3f;
        float leftAlpha = 0.6f + 0.4f * (1 - scale);

        ViewHelper.setTranslationX(mMainView, mMenuWidth * scale * 0.8f);
        ViewHelper.setPivotX(mMainView, 0);
        ViewHelper.setPivotY(mMainView, mMainView.getHeight() / 2);

        ViewHelper.setScaleX(mMenuView, leftScale);
        ViewHelper.setScaleY(mMenuView, leftScale);
        //设置透明度
        ViewHelper.setAlpha(mMenuView, leftAlpha);

        ViewHelper.setScaleX(mMainView, rightScale);
        ViewHelper.setScaleY(mMainView, rightScale);

    }
}
